package com.sgcc.ln.pmos.e_ln_mall.presenter;

import com.blankj.utilcode.util.SPUtils;
import com.sgcc.ln.pmos.common.base.BasePresenter;
import com.sgcc.ln.pmos.common.bean.BaseResponse;
import com.sgcc.ln.pmos.common.bean.PackageBean;
import com.sgcc.ln.pmos.common.http.HttpInterConst;
import com.sgcc.ln.pmos.common.http.HttpRequestUtils;
import com.sgcc.ln.pmos.common.http.MvpNetCallback;
import com.sgcc.ln.pmos.e_ln_mall.bean.MallGoodsBean;
import com.sgcc.ln.pmos.e_ln_mall.bean.PackageDetailsBean;
import com.sgcc.ln.pmos.e_ln_mall.bean.PackageInfoBean;
import com.sgcc.ln.pmos.e_ln_mall.bean.PriceAndPriceDetailBean;
import com.sgcc.ln.pmos.e_ln_mall.bean.SchemeCombBySchemeIdYHBean;
import com.sgcc.ln.pmos.e_ln_mall.contract.PackageDetailsContract;
import com.sgcc.ln.pmos.e_ln_personalcenter.bean.CollectionBean;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class PackageDetailsPresenter extends BasePresenter<PackageDetailsContract.View> implements PackageDetailsContract.Presenter {

    @Override
    public void getGoodsList(String combName) {
        Map<String, Object> params = new HashMap<>();
        params.put("pageNum", 1);
        params.put("pageSize", 5);
        params.put("combName", combName);
        params.put("ratioId", SPUtils.getInstance().getString("ratioId", "1"));
        params.put("membersSdId", "");
        params.put("sort", "");
        params.put("soldEnergySort", "");
        params.put("avgPriceSort", "");
        params.put("createDateSort", "");
        Map<String, Object> params1 = new HashMap<>();
        params.put("packageInfoCombVo", params1);
        params.put("_t", System.currentTimeMillis());
        HttpRequestUtils.getInstance().request(params, HttpInterConst.HOME_PACKAGE, new MvpNetCallback<MallGoodsBean>(getView(), true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁
                if (!isViewAttached()) {
                    return;
                }
                getView().queryGoodsFailed();
            }

            @Override
            public void onResponse(MallGoodsBean response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                getView().queryGoodsSuccess(response);
            }
        });
    }

    @Override
    public void getGoodsDetails(String combName) {
        Map<String, Object> params = new HashMap<>();
        params.put("_t", System.currentTimeMillis());
        params.put("ustatus", "01");
        params.put("ptype", "1");
        params.put("ratioId", SPUtils.getInstance().getString("ratioId", "1"));
        params.put("membersUserId", "");
        params.put("membersSdId", "");
        HttpRequestUtils.getInstance().request(params, HttpInterConst.USER_COLLECTION, new
                MvpNetCallback<CollectionBean>(getView(), true) {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        super.onError(call, e, id);
                        // 失败回调 判断界面是否销毁
                        if (!isViewAttached()) {
                            return;
                        }
                        getView().getGoodsDetailsFailed();
                    }

                    @Override
                    public void onResponse(CollectionBean response, int id) {
                        super.onResponse(response, id);
                        if (!isViewAttached()) {
                            return;
                        }
                        if (null != response) {
                            if (null != response.getRows() && response.getRows().size() > 0) {
                                for (PackageBean row : response.getRows()) {
                                    if (row.getCombCode().equals(combName)) {
                                        getView().getGoodsDetailsSuccess(row);
                                        break;
                                    }

                                }
                            }
                        }
                    }
                });


    }

    @Override
    public void firstCheckOrder(String combId, String isImmediate) {
        Map<String, Object> params = new HashMap<>();
        params.put("combId", combId);
        params.put("isImmediate", isImmediate);
        params.put("_t", System.currentTimeMillis());
        HttpRequestUtils.getInstance().request(params, HttpInterConst.CHECK_ORDER, new MvpNetCallback<BaseResponse>(getView(), true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁
                if (!isViewAttached()) {
                    return;
                }
                getView().firstOrderCheckFailed();
            }

            @Override
            public void onResponse(BaseResponse response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                getView().firstOrderCheckSuccess(response);
            }
        });
    }

    @Override
    public void getPackageDetails(String combId) {
        Map<String, Object> params = new HashMap<>();
        params.put("combId", combId);
        params.put("_t", System.currentTimeMillis());
        HttpRequestUtils.getInstance().request(params, HttpInterConst.MALL_PACKAGECOMB, new MvpNetCallback<PackageDetailsBean>(getView(), true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁
                if (!isViewAttached()) {
                    return;
                }
                getView().queryPackageDetailsFailed();
            }

            @Override
            public void onResponse(PackageDetailsBean response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                getView().queryPackageDetailsSuccess(response);
            }
        });
    }

    @Override
    public void getPackageInfo(String combId) {
        Map<String, Object> params = new HashMap<>();
        params.put("combId", combId);
        params.put("effectiveDate", "");
        params.put("invalidDate", "");
        params.put("_t", System.currentTimeMillis());
        HttpRequestUtils.getInstance().request(params, HttpInterConst.MALL_GETPACKAGEINFO, new MvpNetCallback<PackageInfoBean>(getView(), true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁
                if (!isViewAttached()) {
                    return;
                }
                getView().queryMonthListFailed();
            }

            @Override
            public void onResponse(PackageInfoBean response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                getView().queryMonthListSuccess(response);
            }
        });
    }

    @Override
    public void getSchemeCombBySchemeIdYH(String mainSchemeId, String floSchemeId) {
        Map<String, Object> params = new HashMap<>();
        params.put("mainSchemeId", mainSchemeId);
        params.put("floSchemeId", floSchemeId);
        params.put("_t", System.currentTimeMillis());
        HttpRequestUtils.getInstance().request(params, HttpInterConst.MALL_GETSCHEMECOMBBYSCHEMEIDYH, new MvpNetCallback<SchemeCombBySchemeIdYHBean>(getView(), true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁
                if (!isViewAttached()) {
                    return;
                }
                getView().queryPriceListFailed();
            }

            @Override
            public void onResponse(SchemeCombBySchemeIdYHBean response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                getView().queryPriceListSuccess(response);
            }
        });
    }

    @Override
    public void getPriceAndPriceDetail(String mainSchemeId, String floSchemeId) {
        Map<String, Object> params = new HashMap<>();
        params.put("mainSchemeId", mainSchemeId);
        params.put("floSchemeId", floSchemeId);
        params.put("_t", System.currentTimeMillis());
        HttpRequestUtils.getInstance().request(params, HttpInterConst.MALL_GETPRICEANDPRICEDETAILYH, new MvpNetCallback<PriceAndPriceDetailBean>(getView(), true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁
                if (!isViewAttached()) {
                    return;
                }
                getView().queryDetailFailed();
            }

            @Override
            public void onResponse(PriceAndPriceDetailBean response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                getView().queryDetailSuccess(response);
            }
        });
    }


    /**
     * 加入收藏
     *
     * @param membersUserName
     * @param membersSdId
     * @param membersSdName
     * @param combId
     * @param combName
     */
    @Override
    public void addUserPackageCollection(String membersUserName, String membersSdId, String membersSdName, String combId, String combName) {
        Map<String, Object> params = new HashMap<>();
        params.put("membersUserName", membersUserName);
        params.put("membersSdId", membersSdId);
        params.put("membersSdName", membersSdName);
        params.put("ptype", "1");
        params.put("ustatus", "01");
        params.put("combId", combId);
        params.put("combName", combName);
        params.put("remark", "");
        params.put("_t", System.currentTimeMillis());
        HttpRequestUtils.getInstance().request(params, HttpInterConst.MALL_PACKAGE_COLLECTION, new MvpNetCallback<BaseResponse>(getView(), true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁
                if (!isViewAttached()) {
                    return;
                }
                getView().addCollectionFailed();
            }

            @Override
            public void onResponse(BaseResponse response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                getView().addCollectionSuccess(response);
            }
        });
    }

    /**
     * 取消收藏商品/店铺
     *
     * @param combId      套餐ID
     * @param membersSdId 店铺Id
     */
    @Override
    public void deletePackageById(String combId, String membersSdId) {
        Map<String, Object> params = new HashMap<>();
        params.put("ptype", "1");
        params.put("updatePersonId", "");
        params.put("combId", combId);
        params.put("membersSdId", membersSdId);
        params.put("_t", System.currentTimeMillis());
        HttpRequestUtils.getInstance().request(params, HttpInterConst.MALL_PACKAGE_DELE_COLLECTION, new MvpNetCallback<BaseResponse>(getView(), true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁
                if (!isViewAttached()) {
                    return;
                }
                getView().deleteCollectionFailed();
            }

            @Override
            public void onResponse(BaseResponse response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                getView().deleteCollectionSuccess(response);
            }
        });
    }
}
